package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwdcT;
import cn.e4j.modular.crm.entity.GwdczT;
import cn.e4j.modular.crm.mapper.GwdcTMapper;
import cn.e4j.modular.system.entity.SysOrg;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 非组合促销活动维护 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-28
 */
@Service
public class Gwdc003ServiceImpl extends ServiceImpl<GwdcTMapper, GwdcT> {

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdclist(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        //渠道权限
        dto.put("GWAES", sysAuthorityService.getGwaesAuthority(dto));
//        List<Map<String, Object>> gwdcDtos = this.baseMapper.listGwdcPage(page, dto);
        List<Map<String, Object>> gwdcDtos = InvokeUtil.invokeBeanMethod("gwdcTMapper,listGwdcPage",page,dto);
        return gwdcDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdcData(Dto dto) {
        List<Map<String, Object>> gwdcDtos = this.baseMapper.getGwdclist(dto);
        return gwdcDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdczData(Dto dto) {
        List<Map<String, Object>> gwdczDtos = this.baseMapper.getGwdczlist(dto);
        return gwdczDtos;
    }

    @Transactional(readOnly = true)
    public Dto getAddData(Dto dto) {
        //当前登录者及部门
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("GWDC011", shiroUser.getID());
        dto.put("GWDC011_NAME", shiroUser.getNAME());
        dto.put("GWDC012", shiroUser.getORG_ID());
        SysOrg sysOrg = this.getOrg(shiroUser.getORG_ID());
        dto.put("GWDC012_NAME", sysOrg.getNAME());

        Date date = new Date();
        dto.put("GWDC008", DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd));
        dto.put("GWDC009", DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd));
        dto.put("GWDC001", "CXHD" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss));
        return dto;
    }

    public SysOrg getOrg(String id) {
        SysOrg sysOrg = this.baseMapper.getOrgInfo(id);
        return sysOrg;
    }

    @Transactional(readOnly = true)
    public Dto getGwdc023(Dto dto) {
        //Dto outDto = this.baseMapper.getGwdb1(dto);
        Dto outDto = InvokeUtil.invokeBeanMethod("gwdcTMapper,getGwdb1",dto);
        if (outDto == null && dto.getString("GWDC007").equals("2")) {
            //outDto = this.baseMapper.getGwdb2(dto);
            outDto = InvokeUtil.invokeBeanMethod("gwdcTMapper,getGwdb2",dto);
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    public Dto getZpGwdc023(Dto dto) {
        Dto outDto = this.baseMapper.getGwdb1(dto);
        if (outDto == null && dto.getString("GWDC007").equals("2")) {
            outDto = this.baseMapper.getGwdb2(dto);
        }
        if (outDto == null) {
            outDto = this.baseMapper.getGwdb3(dto);
        }
        return outDto;
    }

    @Transactional
    public Dto addData(List<GwdcT> gwdcTList, List<GwdczT> gwdczTList, String gwdc001) {
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        ShiroUser shiroUser = ShiroKit.getUser();
        Date date = new Date();
        String GWDCTIME = DateUtils.formatDate(date, "HH:mm:ss");
        for (GwdcT gwdcT : gwdcTList) {
            if (gwdcT.getGWDC022().equals("N")) {//非阶梯，客户+产品不允许重复
                QueryWrapper<GwdcT> gwdcTQueryWrapper = new QueryWrapper<>();
                gwdcTQueryWrapper.eq("GWDCENT", shiroUser.getDBC());
                gwdcTQueryWrapper.eq("GWDC001", gwdcT.getGWDC001());
                gwdcTQueryWrapper.eq("GWDC016", gwdcT.getGWDC016());
                gwdcTQueryWrapper.eq("GWDC018", gwdcT.getGWDC018());
                int query_count = this.baseMapper.selectList(gwdcTQueryWrapper).size();
                if (query_count > 0) {
                    outDto.setAppCode(401);
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return outDto;
                }
            }
            gwdcT.setGWDCENT(shiroUser.getDBC());
            gwdcT.setGWDCACTI("N");
            gwdcT.setGWDCTIME(GWDCTIME);
            gwdcT.insert();
        }
        GwdcT gwdc = new GwdcT();
        gwdc.setGWDC001(gwdc001);
        gwdc.setGWDCTIME(GWDCTIME);
        gwdc.setGWDCENT(shiroUser.getDBC());
        UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
        gwdcTUpdateWrapper.eq("GWDCENT", gwdc.getGWDCENT());
        gwdcTUpdateWrapper.eq("GWDC001", gwdc001);
        gwdc.update(gwdcTUpdateWrapper);
        for (GwdczT gwdczT : gwdczTList) {
            Dto query_gwdcz = Dtos.newDto();
            query_gwdcz.put("GWDCZENT", shiroUser.getDBC());
            query_gwdcz.put("GWDCZ001", gwdc001);
            query_gwdcz.put("GWDCZ002", gwdczT.getGWDCZ002());
            query_gwdcz.put("GWDCZ004", gwdczT.getGWDCZ004());
            int query_count = this.baseMapper.getGwdcz(query_gwdcz).size();
            if (query_count > 0) {
                outDto.setAppCode(402);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            }
            gwdczT.setGWDCZENT(shiroUser.getDBC());
            gwdczT.setGWDCZ001(gwdc001);
            gwdczT.insert();
        }
        return outDto;
    }

    @Transactional
    public Dto editData(List<GwdcT> gwdcTList, List<GwdczT> gwdczTList, String gwdc001) {
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        ShiroUser shiroUser = ShiroKit.getUser();

        GwdcT gwdcT_del = new GwdcT();
        UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
        gwdcTUpdateWrapper.eq("GWDCENT", shiroUser.getDBC());
        gwdcTUpdateWrapper.eq("GWDC001", gwdc001);
        gwdcT_del.delete(gwdcTUpdateWrapper);

        GwdczT gwdczT_del = new GwdczT();
        UpdateWrapper<GwdczT> gwdczTUpdateWrapper = new UpdateWrapper<>();
        gwdczTUpdateWrapper.eq("GWDCZENT", shiroUser.getDBC());
        gwdczTUpdateWrapper.eq("GWDCZ001", gwdc001);
        gwdczT_del.delete(gwdczTUpdateWrapper);

        Date date = new Date();
        String GWDCTIME = DateUtils.formatDate(date, "HH:mm:ss");
        for (GwdcT gwdcT : gwdcTList) {
            if (gwdcT.getGWDC022().equals("N")) {//非阶梯，客户+产品不允许重复
                QueryWrapper<GwdcT> gwdcTQueryWrapper = new QueryWrapper<>();
                gwdcTQueryWrapper.eq("GWDCENT", shiroUser.getDBC());
                gwdcTQueryWrapper.eq("GWDC001", gwdcT.getGWDC001());
                gwdcTQueryWrapper.eq("GWDC016", gwdcT.getGWDC016());
                gwdcTQueryWrapper.eq("GWDC018", gwdcT.getGWDC018());
                int query_count = this.baseMapper.selectList(gwdcTQueryWrapper).size();
                if (query_count > 0) {
                    outDto.setAppCode(401);
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return outDto;
                }
            }
            gwdcT.setGWDCENT(shiroUser.getDBC());
            gwdcT.setGWDCACTI("N");
            gwdcT.setGWDCTIME(GWDCTIME);
            gwdcT.insert();
        }
        for (GwdczT gwdczT : gwdczTList) {
            Dto query_gwdcz = Dtos.newDto();
            query_gwdcz.put("GWDCZENT", shiroUser.getDBC());
            query_gwdcz.put("GWDCZ001", gwdc001);
            query_gwdcz.put("GWDCZ002", gwdczT.getGWDCZ002());
            query_gwdcz.put("GWDCZ004", gwdczT.getGWDCZ004());
            int query_count = this.baseMapper.getGwdcz(query_gwdcz).size();
            if (query_count > 0) {
                outDto.setAppCode(402);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            }
            gwdczT.setGWDCZENT(shiroUser.getDBC());
            gwdczT.setGWDCZ001(gwdc001);
            gwdczT.insert();
        }
        return outDto;
    }

    @Transactional
    public void delete(Dto dto) {
        GwdcT gwdcT = new GwdcT();
        UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
        gwdcTUpdateWrapper.eq("GWDCENT", dto.getString("GWDCENT"));
        gwdcTUpdateWrapper.eq("GWDC001", dto.getString("GWDC001"));

        GwdczT gwdczT = new GwdczT();
        UpdateWrapper<GwdczT> gwdczTUpdateWrapper = new UpdateWrapper<>();
        gwdczTUpdateWrapper.eq("GWDCZENT", dto.getString("GWDCENT"));
        gwdczTUpdateWrapper.eq("GWDCZ001", dto.getString("GWDC001"));

        gwdcT.delete(gwdcTUpdateWrapper);
        gwdczT.delete(gwdczTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwdcT> gwdcTList) {
        for (GwdcT gwdcT : gwdcTList) {
            UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
            gwdcTUpdateWrapper.eq("GWDCENT", gwdcT.getGWDCENT());
            gwdcTUpdateWrapper.eq("GWDC001", gwdcT.getGWDC001());

            GwdczT gwdczT = new GwdczT();
            UpdateWrapper<GwdczT> gwdczTUpdateWrapper = new UpdateWrapper<>();
            gwdczTUpdateWrapper.eq("GWDCZENT", gwdcT.getGWDCENT());
            gwdczTUpdateWrapper.eq("GWDCZ001", gwdcT.getGWDC001());

            gwdcT.delete(gwdcTUpdateWrapper);
            gwdczT.delete(gwdczTUpdateWrapper);
        }
    }

    @Transactional
    public void updateData(List<GwdcT> gwdcTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwdcT gwdcT : gwdcTList) {

            if (gwdcT.getGWDCACTI().equals("Y")) {
                gwdcT.setGWDC013(shiroUser.getID());
                gwdcT.setGWDC014(DateUtils.formatDate(new Date(), DateUtils.PATTERN_yyyyMMdd));
            } else {
                gwdcT.setGWDC013("");
                gwdcT.setGWDC014("");
            }

            UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
            gwdcTUpdateWrapper.eq("GWDCENT", gwdcT.getGWDCENT());
            gwdcTUpdateWrapper.eq("GWDC001", gwdcT.getGWDC001());
            gwdcT.update(gwdcTUpdateWrapper);
        }
    }

    @Transactional
    public void editGwdc010(GwdcT gwdcT) {
        UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
        gwdcTUpdateWrapper.eq("GWDCENT", gwdcT.getGWDCENT());
        gwdcTUpdateWrapper.eq("GWDC001", gwdcT.getGWDC001());
        gwdcT.update(gwdcTUpdateWrapper);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdc001Data(Page<Object> page, Dto dto) {
//        List<Map<String, Object>> listData = this.baseMapper.getGwdc001Data(page, dto);
        List<Map<String, Object>> listData = InvokeUtil.invokeBeanMethod("gwdcTMapper,getGwdc001Data",page,dto);
        return listData;
    }

}
